package de.staxx6.bomber.box;

import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;

import de.staxx6.bomber.GameState;
import de.staxx6.bomber.Stats;

public class ExplosionBox extends BoxBomber{
	
	private Node fieldNode = new Node("Explosion Node");
	private final int index;
	private boolean isTimer = false;
	private float timer = 0;
	private boolean isRemoved = false;
	
	public ExplosionBox(final float newX, final float newY, Node newFieldNode, boolean newTimer) {
		super(newX, newY, "ExplosionBox");
		this.fieldNode = newFieldNode;
		this.index = Stats.BOMBINDEX;
		this.isTimer = newTimer;
		Stats.BOMBINDEX++;
		
		createBox();
		System.out.println("Explosion!@ X:" + x + " Y: " + y);
	}
	
	public ExplosionBox(final float newX, final float newY, Node newFieldNode) {
		super(newX, newY, "ExplosionBox");
		this.fieldNode = newFieldNode;
		this.index = Stats.BOMBINDEX;
		Stats.BOMBINDEX++;
		
		createBox();
		System.out.println("Explosion!@ X:" + x + " Y: " + y);
	}
	
	private void createBox() {
		Geometry geo_Box = new Geometry("Explosion", box);
		Material mat_Box = new Material(GameState.ASSETMANAGER, "Common/MatDefs/Misc/Unshaded.j3md");
		mat_Box.setColor("Color", ColorRGBA.Red);
		geo_Box.setMaterial(mat_Box);
		boxNode.attachChild(geo_Box);
		fieldNode.attachChild(boxNode);
	}
	
	public void update(float delta) {
		/* Timer */
		if(isTimer) {
			if(timer >= Stats.EXPLOSIONENDTIMER) {
				remove();
			} else {
				timer++;
			}
		}
	}
	
	private void remove() {
		fieldNode.detachChild(boxNode);
		boxNode.removeFromParent();
		isRemoved = true;
	}
	
	public boolean isRemoved() {
		return isRemoved;
	}
	
	public int getIndex() {
		return index;
	}
}
